#include "doubleLinkList.h"
LTNode* LTInit()
{
	LTNode* phead = (LTNode*)malloc(sizeof(LTNode));
	phead->pre = phead;
	phead->next = phead;
	return phead;
}
bool LTEmpty(LTNode* phead)
{
	if (phead->next == phead) return true;
	else return false;
}
void LTPrint(LTNode* phead)
{
	LTNode* p = phead->next;
	while (p!= phead)
	{
		printf("%d->", p->data);
		p = p->next;
	}
	printf("null\n");
}
LTNode* buyNode(LTDataType x)
{
	LTNode* new = (LTNode*)malloc(sizeof(LTNode));
	new->data = x;
	new->next = new;
	new->pre = new;
	return new;
}
void LTPushBack(LTNode* phead, LTDataType x)
{
	LTNode* newnode = buyNode(x);
	phead->pre->next = newnode;
	newnode->pre = phead->pre;
	phead->pre = newnode;
	newnode->next= phead;
}
void LTPopBack(LTNode* phead)
{
	assert(phead->next != phead);
	LTNode* p = phead->pre;
	phead->pre = phead->pre->pre;
	phead->pre->next = phead;
	free(p);
	p = NULL;
}

void LTPushFront(LTNode* phead, LTDataType x)
{
	LTNode* newnode = buyNode(x);
	phead->next->pre = newnode;
	newnode->next = phead->next;
	phead->next = newnode;
	newnode->pre = phead;
}

void LTPopFront(LTNode* phead)
{
	LTNode* p = phead->next;
	phead->next = p->next;
	p->next->pre = phead;
	free(p);
	p = NULL;
}

LTNode* LTFind(LTNode* phead, LTDataType x)
{
	LTNode* p = phead->next;
	while (p != phead)
	{
		if (p->data == x)
		{
			//printf("%d", p->data);
			return p;
		}
		p = p->next;
	}
	return NULL;
}

void LTInsert(LTNode* pos, LTDataType x)
{
	LTNode* new = buyNode(x);
	pos->next->pre = new;
	new->next=pos->next;
	pos->next = new;
	new->pre = pos;
}

void LTErase(LTNode* pos)
{
	pos->pre->next = pos->next;
	pos->next->pre = pos->pre;
	free(pos);
	pos = NULL;
}